﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace ToyundaPhone.Pages
{
	public partial class PlaylistsPage : PhoneApplicationPage, System.ComponentModel.INotifyPropertyChanged
	{
		public PlaylistsPage()
		{
			InitializeComponent();
		}

		#region INotifyPropertyChanged Members

		public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

		#endregion

		public IEnumerable<Database.Playlist> Playlists { get; private set; }

		private void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
			}
		}

		private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
		{
			UpdatePlaylists();
		}

		private void UpdatePlaylists()
		{
			Database.SongDatabase db = (Application.Current as App).SongDatabase;
			Playlists = from p in db.Playlists orderby p.Name select p;
			OnPropertyChanged("Playlists");
		}

		private void Playlist_Tap(object sender, System.Windows.Input.GestureEventArgs e)
		{
			e.Handled = true;
			Database.Playlist pl = (sender as FrameworkElement).DataContext as Database.Playlist;
			NavigationService.Navigate(new Uri(string.Format("/playlist/{0}", pl.Id), UriKind.Relative));
		}

		private void Delete_Click(object sender, RoutedEventArgs e)
		{
			Database.Playlist pl = (sender as FrameworkElement).DataContext as Database.Playlist;
			if (pl != null)
			{
				Database.SongDatabase db = (Application.Current as App).SongDatabase;
				db.Playlists.DeleteOnSubmit(pl);
				db.SubmitChanges();
				UpdatePlaylists();
			}
		}

		private void New_Click(object sender, EventArgs e)
		{
			InputScope scope = new InputScope();
			scope.Names.Add(new InputScopeName() { NameValue = InputScopeNameValue.Text });
			PhoneTextBox name = new PhoneTextBox()
			{
				Hint = "nom de la liste",
				InputScope = scope
			};
			CustomMessageBox mbox = new CustomMessageBox()
			{
				Title = "listes",
				Caption = "Nouvelle liste",
				Content = name,
				LeftButtonContent = "ajouter",
				RightButtonContent = "annuler"
			};
			Action create = () =>
			{
				Database.SongDatabase db = (Application.Current as App).SongDatabase;
				Database.Playlist pl = new Database.Playlist();
				pl.Name = name.Text;
				db.Playlists.InsertOnSubmit(pl);
				db.SubmitChanges();
				UpdatePlaylists();
			};
			name.KeyUp += (s, arg) =>
			{
				if (arg.Key == Key.Enter)
				{
					arg.Handled = true;
					mbox.Dismiss();
					if (name.Text.Length > 0)
					{
						create();
					}
				}
			};
			mbox.Dismissed += (s, arg) =>
			{
				if ((arg.Result == CustomMessageBoxResult.LeftButton) && (name.Text.Length > 0))
				{
					create();
				}
			};
			mbox.Show();
		}
	}
}